
/* GCSx
** SPAWN.CPP
**
** Spawn-point support (starting point for sprite/script)
** Doesn't include any editor-only functionality
** Usually simply referred to as sprites/objects/scripts in the editor
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

Spawn::Spawn(int newId) : name(blankString) { start_func
    script = NULL;
    animgroup = NULL;
    tileset = NULL;
    subid = 0;
    x = y = 0;
    id = newId;
}

Spawn::~Spawn() { start_func
}

void Spawn::load(class FileRead* file, const World* world) throw_File { start_func
    id = file->readInt();
    file->readStr(name);
    
    int code = file->readInt();
    if (code) {
        script = world->findScript(code);
        if (!script) throw FileException("Corrupted spawn-point content");
    }
    else script = NULL;
    
    code = file->readInt();
    if (code) {
        animgroup = world->findAnimGroup(code);
        if (!animgroup) throw FileException("Corrupted spawn-point content");
    }
    else animgroup = NULL;
    
    code = file->readInt();
    if (code) {
        tileset = world->findTileSet(code);
        if (!tileset) throw FileException("Corrupted spawn-point content");
    }
    else tileset = NULL;
        
    subid = file->readInt();
    x = file->readInt();
    y = file->readInt();
    
    if (((animgroup) && (tileset)) ||
        ((subid) && (!animgroup) && (!tileset)) ||
        ((subid <= 0) && ((animgroup) || (tileset))) ||
        (!id))
        throw FileException("Corrupted spawn-point content");
}

void Spawn::generate(Layer* toLayer, WorldPlay* toWorld) { start_func
    assert(toWorld);
    Sprite* spr = NULL;
    
    if (toLayer) {
        spr = new Sprite(toWorld->unusedSpriteId());
        if (tileset)
            spr->setImage(tileset, subid);
        else if (animgroup)
            spr->setImage(animgroup, subid, 1);
        else
            spr->setImage();
            
        spr->moveTo(x, y);
        toLayer->indexSprite(spr);
    }

    if (script) {
        Entity* obj = new Entity(toWorld->unusedEntityId());
        obj->setName(name);
        obj->setSprite(spr);
        obj->setScript(script);
        spr->setEntity(obj);
        
        if (toLayer)
            toLayer->getScene()->indexEntity(obj);
        else
            toWorld->indexEntity(obj);
    }
}
